<?php

/**
 * WHQ DB model
 *
 * @author Milan Rukavina <rukavinamilan@gmail.com>
 * @version 0.1
 * @package whq
 */
class WHQ_Model_Db
{
	/**
	 * Singleton instance
	 * @var WHQ_Model_Db
	 */
	protected static $_instance = null;
    
	private $dbHost = '';
    private $dbUser = '';
    private $dbPass = '';
    private $dbName = '';
    private $dbConn = null;

/**
 * returns singleton instance of WHQ_Model_Db
 * @return WHQ_Model_Db
 */
	public static function getInstance($params = array())
	{
		if(self::$_instance === null)
		{
			self::$_instance = new self($params['db_host'],$params['db_user'],$params['db_password'],$params['db_name']);
		}
		return self::$_instance;
	}

    /**
     * Construct model object class
     * 
     * @param string $host
     * @param string $user
     * @param string $pass
     * @param string $db
     */
    private function __construct($host,$user,$pass,$db){
        $this->dbHost = $host;
        $this->dbUser = $user;
        $this->dbPass = $pass;
        $this->dbName = $db;

        $this->_connect();
    }

    /**
     * destructor
     */
    public function  __destruct() {
        $this->_disconnect();
    }


    /**
     * open db connection
     * 
     * @return bool
     */
    private function _connect(){
        $this->dbConn = mysql_connect($this->dbHost, $this->dbUser, $this->dbPass);
        if($this->dbConn){
            return mysql_select_db($this->dbName,$this->dbConn);
        }
        else{
            return false;
        }
    }

    /**
     * close db connection
     */
    private function _disconnect(){
        if($this->dbConn){
            mysql_close($this->dbConn);
            unset($this->dbConn);
        }
    }

    private function execSQL($sql){
        if($this->dbConn){
            $result = mysql_query($sql, $this->dbConn);
            $err = mysql_error($this->dbConn);
            $debugInfo = debug_backtrace();
            return $result;
        }
        else{
            return false;
        }
    }

    /**
     * get all queues
     * @return array
     */
    public function getQueues(){


        $queues = array();
        $sql = "    SELECT *
                    FROM queues
                    ORDER BY name";
        //echo "\n$sql\n\n";
        $result = $this->execSQL($sql);
        if($result && mysql_num_rows($result)){
            while($l = mysql_fetch_assoc($result)){
                $queues[] = $l;
            }
        }

        return $queues;
    }

    /**
     * get subscriptions for a queue
     * @return array
     */
    public function getSubscriptions($queueName){


        $subscriptions = array();
        $sql = "    SELECT s.id, s.uri, s.posted
                    FROM subscriptions AS s
                        LEFT JOIN queues AS q ON q.id = s.queue_id
                    WHERE q.name = '" . addslashes($queueName) . "'
                    ORDER BY s.id";
        //echo "\n$sql\n\n";
        $result = $this->execSQL($sql);
        if($result && mysql_num_rows($result)){
            while($l = mysql_fetch_assoc($result)){
                $subscriptions[] = $l;
            }
        }

        return $subscriptions;
    }

    
}
